package com.example.qxfw.slhx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dmgis.entity.DmMapPoint;
import com.example.qxfw.common.entity.zy.BaseProduct;
import com.example.qxfw.common.entity.zy.HxyjWarning;
import com.example.qxfw.common.util.DmgisUtils;
import com.example.qxfw.slhx.mapper.HxyjWarningMapper;
import com.example.qxfw.slhx.service.HxlsService;
import com.example.qxfw.mongodb.hy.entity.HyDocFile;
import com.example.qxfw.ybzz.mapper.BaseProductMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class HxlsServiceImpl implements HxlsService {
    @Autowired
    private HxyjWarningMapper hxyjWarningMapper;

    @Autowired
    private BaseProductMapper baseProductMapper;

    @Qualifier("mongoSecondTemplate")
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<HxyjWarning> getAllOfHxyjWarning(Timestamp startDate, Timestamp endDate, Integer state) {
        List<HxyjWarning> list = hxyjWarningMapper.selectList(new LambdaQueryWrapper<HxyjWarning>()
                .between(HxyjWarning::getDatechar, startDate, endDate)
                .eq(ObjectUtils.isNotEmpty(state),HxyjWarning::getState,state)
                .orderBy(true,false,HxyjWarning::getDocid));

        for (HxyjWarning hxyjWarning : list) {
            String pos = hxyjWarning.getPos();
            String[] sections = pos.split("\\*");
            StringBuilder sb = new StringBuilder();
            for (String section : sections) {
                String[] parts = section.split("#");
                String coordinates = parts[1].trim();
                String[] xyPairs = coordinates.split(",");
                List<DmMapPoint> wgsPoints = new ArrayList<>();

                for (String xyPair : xyPairs) {
                    String[] xy = xyPair.trim().split("\\s+");
                    double lon = Double.parseDouble(xy[0]);
                    double lat = Double.parseDouble(xy[1]);
                    DmMapPoint wgsPoint = new DmMapPoint(lon, lat);
                    wgsPoints.add(wgsPoint);
                }
                sb.append(parts[0]).append("#");
                for (DmMapPoint wgsPoint : wgsPoints) {
                    sb.append(wgsPoint.getX()).append(" ").append(wgsPoint.getY()).append(",");
                }
                sb.setLength(sb.length() - 1);
                sb.append("*");
            }
            sb.setLength(sb.length() - 1); // 删除最后一个 "*"
            hxyjWarning.setPos(sb.toString());
        }

        return list;
    }

    @Override
    public HxyjWarning updateState(Integer docId, Integer state) {
        HxyjWarning hxyjWarning = new HxyjWarning();
        hxyjWarning.setDocid(docId);
        hxyjWarning.setState(state);
        hxyjWarningMapper.updateById(hxyjWarning);
        return hxyjWarningMapper.selectById(docId);
    }

    @Override
    public Integer getProductId(Integer docId, String docPath) {
        List<BaseProduct> baseProducts = baseProductMapper.selectList(Wrappers.<BaseProduct>lambdaQuery()
                .eq(BaseProduct::getBindid, docId)
                .eq(BaseProduct::getProductname, docPath));
        if (baseProducts.size()>0){
            return baseProducts.get(0).getId();
        } else {
            return null;
        }

    }


    @Override
    public int deleteByIdOfHxyjWarning(Integer docId) {
        HxyjWarning hxyjWarning = hxyjWarningMapper.selectById(docId);
        int row = hxyjWarningMapper.delete(new LambdaQueryWrapper<HxyjWarning>()
                .eq(docId != null,HxyjWarning::getDocid, docId));
        if (hxyjWarning!=null&&ObjectUtils.isNotEmpty(hxyjWarning.getDocid()) && StringUtils.isNotBlank(hxyjWarning.getDocpath())){
            BaseProduct baseProduct = baseProductMapper.selectOne(Wrappers.<BaseProduct>lambdaQuery()
                    .eq(BaseProduct::getBindid, hxyjWarning.getDocid())
                    .eq(BaseProduct::getProductname,hxyjWarning.getDocpath()));
            baseProductMapper.delete(Wrappers.<BaseProduct>lambdaQuery()
                    .eq(BaseProduct::getBindid, hxyjWarning.getDocid())
                    .eq(BaseProduct::getProductname,hxyjWarning.getDocpath()));
            if (baseProduct!=null && ObjectUtils.isNotEmpty(baseProduct.getId())){
                mongoTemplate.remove(new Query(
                        Criteria.where("id").is(baseProduct.getId())
                                .and("table_name").is("base_product")), HyDocFile.class);
            }
        }


        return row;
    }
}





